
/**
 * Jun 1, 2010
 * Jiadong Zhu
 * jiadong.zhu@exigenservices.com
 */

package taihootech.qa.th.simulator;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class SeleniumSimulator implements ISimulator {
	
	private static SeleniumSimulator simulator = null;
	private Selenium selenium = null;
	
	private String host;
	private int port;
	private String browser_type;
	private String url;
	
	private SeleniumSimulator() {}
	
	public static ISimulator getInstance() {
		if (simulator == null)
			simulator = new SeleniumSimulator();
		return simulator;
	}///:~
	
	public Selenium getSelenium() {
		if (this.selenium == null)
			this.start();
		return this.selenium;
	}///:~

	@Override
	public boolean start() {
		boolean code = true;
		try {
			if (this.selenium != null)
				this.stop();
			setPlatform(host, port, browser_type, url);
			this.selenium.start();
		}catch(Exception e) {
			code = false;
			e.printStackTrace();
		}
		return code;
	}///:~

	@Override
	public boolean stop() {
		boolean code = true;
		try {
			if (this.selenium != null) {
				this.selenium.close();
				this.selenium.stop();
			}
		} catch (Exception e) {
			code = false;
			e.printStackTrace();
		}
		return code;
	}///:~
	
	private void setPlatform(String host, int port, String browser_type, String url) {
		this.selenium = new DefaultSelenium(host, port, browser_type, url);
	}///:~

}///: SeleniumSimulator Ends


